/**
 * Copyright &copy; 2018-2018 <a href="#">聚成科技</a> All rights reserved.
 */
package com.jckj.core.controller;

import com.github.pagehelper.PageInfo;
import com.jckj.core.annotation.LogTitle;
import com.jckj.core.contants.SysContants;
import com.jckj.core.entity.OfficeUserKey;
import com.jckj.core.entity.OfficeUserView;
import com.jckj.core.entity.SysUser;
import com.jckj.core.enums.UserStatus;
import com.jckj.core.enums.UserType;
import com.jckj.core.service.OfficeService;
import com.jckj.core.service.UserService;
import com.jckj.core.util.ClassUtils;
import com.jckj.core.util.StringUtils;
import com.jckj.core.vos.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.Calendar;

/**
 * <p>
 *     科室人员管理控制器
 * </p>
 *
 * @author 刘志远
 * @date 18-2-2
 */
@RequestMapping("/admin")
@Controller
public class OfficeUserController {

    private final static Logger LOGGER = LoggerFactory.getLogger(OfficeUserController.class);

    @Autowired
    OfficeService officeService;

    @Autowired
    LoginUserVo loginUserVo;

    @Autowired
    UserService userService;

    /**
     * 导向到人员列表
     * @return
     */
    @RequestMapping(value = "officeUserList", method = RequestMethod.GET)
    public ModelAndView officeUserList(){
        ModelAndView modelAndView = new ModelAndView("officeUserList");
        modelAndView.addObject("offices", officeService.listAll());
        return modelAndView;
    }

    /**
     * 查询人员列表
     * @param officeUserQueryVo
     * @return
     */
    @RequestMapping(value = "getUserList", method = RequestMethod.GET)
    @ResponseBody
    public LayPageVo getUserList(OfficeUserQueryVo officeUserQueryVo){
        if(officeUserQueryVo.getLimit()==null||officeUserQueryVo.getLimit()==0){
            officeUserQueryVo.setLimit(SysContants.LIMIT);
        }
        if(officeUserQueryVo.getPage()==null||officeUserQueryVo.getPage()==0){
            officeUserQueryVo.setPage(1);
        }
        PageInfo<OfficeUserView> pageInfo = userService.getOfficeUserList(officeUserQueryVo);
        LayPageVo layPageVo = new LayPageVo();
        layPageVo.setCode(0);
        layPageVo.setCount(pageInfo.getTotal());
        layPageVo.setData(pageInfo.getList());
        return layPageVo;
    }

    /**
     * 导向到新增页面
     * @return
     */
    @RequestMapping(value = "addOfficeUser", method= RequestMethod.GET)
    public ModelAndView addOfficeUser(){
        ModelAndView modelAndView = new ModelAndView("addOfficeUser");
        modelAndView.addObject("offices", officeService.listAll());
        return modelAndView;
    }

    /**
     * 保存科室人员
     * @param officeUserVo
     * @param bindingResult
     * @return
     */
    @RequestMapping(value = "saveOffcieUser", method= RequestMethod.POST)
    @ResponseBody
    public Result saveOffcieUser(@ModelAttribute @Validated OfficeUserVo officeUserVo, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            return Result.invalidParams(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        SysUser sysUser = new SysUser();
        try {
            ClassUtils.copyValue(officeUserVo, sysUser);
        } catch (Exception e) {
            LOGGER.error("新增科室人员时发生错误", e.fillInStackTrace());
            return Result.invalidParams("新增科室人员时发生错误");
        }

        sysUser.setId(StringUtils.genId());
        sysUser.setUserType(UserType.OFFICER.getCode());
        sysUser.setSalt(StringUtils.getSalt());
        sysUser.setPassword(StringUtils.encryPassword("123456", sysUser.getSalt()));
        sysUser.setCreateTime(Calendar.getInstance().getTime());
        sysUser.setCreateBy(loginUserVo.getId());
        sysUser.setUserStatus(UserStatus.NORMAL.getCode());
        OfficeUserKey officeUserKey = new OfficeUserKey();
        officeUserKey.setOfficeId(officeUserVo.getOfficeId());
        officeUserKey.setUserId(sysUser.getId());

        userService.addOfficeUser(sysUser, officeUserKey);

        return Result.okWithMessage("增加成功，密码密码为123456");
    }

    @RequestMapping(value = "/disabledUser", method = RequestMethod.POST)
    @ResponseBody
    @LogTitle(title = "停用科室人员")
    public Result disabledUser(@ModelAttribute @Validated OfficeUserVo officeUserVo, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            return Result.invalidParams(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        officeUserVo.setUpdateBy(loginUserVo.getId());
        officeUserVo.setUpdateTime(Calendar.getInstance().getTime());
        userService.disabledUser(officeUserVo);
        return Result.ok();
    }

    @RequestMapping(value = "/enableUser", method = RequestMethod.POST)
    @ResponseBody
    @LogTitle(title = "启用科室人员")
    public Result enableUser(@ModelAttribute @Validated OfficeUserVo officeUserVo, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            return Result.invalidParams(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        officeUserVo.setUpdateBy(loginUserVo.getId());
        officeUserVo.setUpdateTime(Calendar.getInstance().getTime());
        userService.enableUser(officeUserVo);
        return Result.ok();
    }

    /**
     * 重置科室人员密码
     * @param officeUserVo
     * @param bindingResult
     * @return
     */
    @RequestMapping(value = "/resetOfficeUserPwd", method = RequestMethod.POST)
    @ResponseBody
    @LogTitle(title = "重置科室人员密码")
    public Result resetOfficeUserPwd(@ModelAttribute @Validated OfficeUserVo officeUserVo, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            return Result.invalidParams(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        officeUserVo.setUpdateBy(loginUserVo.getId());
        officeUserVo.setUpdateTime(Calendar.getInstance().getTime());
        userService.resetPassword(officeUserVo);
        return Result.ok();
    }

    @RequestMapping(value = "/updateOfficeUser", method = RequestMethod.POST)
    @ResponseBody
    @LogTitle(title = "更新科室人员信息")
    public Result updateOfficeUser(@ModelAttribute @Validated OfficeUserVo officeUserVo, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            return Result.invalidParams(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        officeUserVo.setUpdateBy(loginUserVo.getId());
        officeUserVo.setUpdateTime(Calendar.getInstance().getTime());
        userService.updateOfficeUser(officeUserVo);
        return Result.ok();
    }

}
